home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / obsolete / xanimate.pro < prev    next >
Text File  |  1997-07-08  |  8KB  |  257 lines

  1. ; $Id: xanimate.pro,v 1.2 1997/01/15 04:02:19 ali Exp $
  2. ;
  3. ; Copyright (c) 1990-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5.  
  6. pro xanimate, set = set, image = image, frame = frame, order = order, $
  7.     close = close, title = title, window = window, rate
  8. ;+
  9. ; NAME:
  10. ;    XANIMATE
  11. ;
  12. ; PURPOSE:
  13. ;    Display an animated sequence of images using Xwindows Pixmaps,
  14. ;    or the SunView display.
  15. ;
  16. ; CATEGORY:
  17. ;    Image display.
  18. ;
  19. ; CALLING SEQUENCE:
  20. ;    To initialize:
  21. ;    XANIMATE, SET = [Sizex, Sizey, Nframes, Show_Window]
  22. ;
  23. ;    To load a single image:
  24. ;    XANIMATE, IMAGE = Image, FRAME = Frame_Index
  25. ;
  26. ;    To load a single image that is already displayed in an existing window:
  27. ;    XANIMATE, FRAME = Frame_Index, WINDOW = [ Window [, X0, Y0, Sx, Sy]]
  28. ;    (This technique is much faster than reading back from the window.)
  29. ;
  30. ;    To display the animation after all the images have been loaded:
  31. ;    XANIMATE [, Rate]
  32. ;    To stop the display, hit any key.
  33. ;
  34. ;    To close and deallocate the pixmap/buffer:
  35. ;    XANIMATE, /CLOSE
  36. ;
  37. ; OPTIONAL INPUT PARAMETERS:
  38. ;    Rate:    The basic display rate in frames per second.  The default 
  39. ;        value is "infinity".  Note, however, that the maximum rate of
  40. ;        display is limited by the speed of your computer and video 
  41. ;        hardware.  The delay between loading successive images is 
  42. ;        1.0/Rate.
  43. ;
  44. ; KEYWORD PARAMETERS:
  45. ;    SET:    A vector of parameters that initialize XANIMATE.  SET should 
  46. ;        be set to a 3- to 5-element integer vector containing the 
  47. ;        following parameters:
  48. ;        Sizex, Sizey:    The X and Y sizes of the images to be 
  49. ;                displayed, in pixels.
  50. ;
  51. ;        Nframes:    The number of frames in the animated sequence.
  52. ;
  53. ;        Show_Window:    The number of the window in which to display 
  54. ;                the animation.  If this parameter is omitted,
  55. ;                window 0 is used.  This parameter is ignored 
  56. ;                for Sun (the current window is used.)
  57. ;
  58. ;    IMAGE:    A 2D array containing an image to be loaded at the position
  59. ;        given by FRAME.  The FRAME keyword must also be specified.
  60. ;
  61. ;    FRAME:    The number of the frame to load an image in.  FRAME must be 
  62. ;        in the range 0 to Nframes-1.  FRAME is used in conjunction with
  63. ;        either the IMAGE or WINDOW keywords.
  64. ;
  65. ;    WINDOW:    The number of an existing window to copy an image from.
  66. ;        When using X windows, this technique is much faster than 
  67. ;        reading from the display and then calling XANIMATE.
  68. ;
  69. ;        The value of this parameter is either a simple window
  70. ;        index (in which case the entire window is copied),
  71. ;        or a 5-element vector containing the window index, the
  72. ;        starting X and Y locations of the area to be copied, and the 
  73. ;        X and Y sizes of the area to be copied (in pixels).
  74. ;
  75. ;    ORDER:    Set this keyword if images are to be displayed from top down.
  76. ;        Omit it or set it to zero if images go from bottom to top.  
  77. ;        This keyword is only used when loading images.
  78. ;
  79. ;    CLOSE:    Set this keyword to delete the pixwin and window and free 
  80. ;        memory.
  81. ;
  82. ;    TITLE:    A string to use as the title of the animation window (X only).
  83. ;
  84. ; OUTPUTS:
  85. ;    No explicit outputs.
  86. ;
  87. ; COMMON BLOCKS:
  88. ;    XANIMATE_COM - private common block.
  89. ;
  90. ; SIDE EFFECTS:
  91. ;    A pixmap and window are created.
  92. ;
  93. ; RESTRICTIONS:
  94. ;    XANIMATE only works for X windows or Sun View.
  95. ;
  96. ;    FOR X:  An animation may not have more than approximately 127 frames.
  97. ;
  98. ;    FOR SUN:  A large 2D memory array is made to contain the images.
  99. ;          For large images or a large number of frames, this procedure
  100. ;          can be a real memory hog.  For SunView, faster operation can
  101. ;          be obtained by using a non-retained window.
  102. ;
  103. ;    Users of X windows can use an improved, "widgetized" version of this
  104. ;    routine called XINTERANIMATE from the IDL widget library.
  105. ;
  106. ; PROCEDURE:
  107. ;    When initialized, this procedure creates an approximately square
  108. ;    pixmap or memory buffer, large enough to contain Nframes of
  109. ;    the requested size.
  110. ;
  111. ;    Once the images are loaded using the FRAME and IMAGE or WINDOW 
  112. ;    keywords, they are displayed by copying the images from the pixmap
  113. ;    or buffer to the visible window.
  114. ;
  115. ; MODIFICATION HISTORY:
  116. ;    DMS, April, 1990.
  117. ;
  118. ;    Modified to use numerous windows under X, DMS, March, 1991.
  119. ;        This mod improves virtual memory allocation and the size of
  120. ;        animations the VAXen can do.
  121. ;-
  122. common xanimate_com, pwin, swin, nframes, xs, ys, nfx, buffer
  123.  
  124. if (!d.name ne 'SUN') and (!d.name ne 'X') then $
  125.     message,'Not a window oriended device.'
  126.  
  127. if keyword_set(close) then begin
  128.     if !d.name eq 'SUN' then begin
  129.         if n_elements(buffer) gt 1 then buffer = 0
  130.         return
  131.         endif
  132.     if n_elements(pwin) le 1 then return
  133.     for i=0,n_elements(pwin)-1 do $
  134.         if pwin(i) ge 0 then wdelete,pwin(i)
  135.     wdelete, swin
  136.     pwin = -1
  137.     return
  138.     endif
  139.  
  140. if keyword_set(set) then begin
  141.     xs = set(0)
  142.     ys = set(1)
  143.     nframes = set(2)
  144.     if n_elements(set) ge 4 then swin = set(3) else swin = 0
  145.     if n_elements(set) ge 5 then $
  146.         message, 'XANIMATE: pixwin param no longer supported'
  147.     if !d.name eq 'SUN' then begin
  148.         s = size(buffer)  ;Existing buffer
  149.         new = 1        ;Make a new image
  150.         if s(0) eq 2 then begin  ;Will previous window do?
  151.           if (s(1) ge xs) and (s(2) ge ys * nframes) then new = 0
  152.         endif
  153.         if new then begin    ;Make new buffer
  154.           buffer = 0        ;Delete old buffer
  155.           buffer = bytarr(xs, ys * nframes, /nozero)
  156.           endif
  157.         return
  158.         endif
  159.     wdelete, swin           ;Remove previous windows
  160.     if n_elements(pwin) gt 1 then $
  161.         for i=0,n_elements(pwin)-1 do $
  162.             if pwin(i) ge 0 then wdelete,pwin(i)
  163.     pwin = replicate(-1, nframes)    ;Pixwin indices
  164.     if n_elements(title) eq 0 then title = 'Xanimate'
  165.     window,retain=0, swin, xsize = xs, ysize = ys, title= title
  166.     wset, swin
  167.     return
  168.     endif
  169.  
  170. ;    Here, windows must exist
  171. if !d.name eq 'X' then begin
  172.     if n_elements(pwin) le 1 then message,"Not initialized"
  173. endif
  174.  
  175. if !d.name eq 'SUN' then $
  176.     if n_elements(buffer) le 1 then $
  177.         message, "Not initialized."
  178.  
  179. if n_elements(frame) gt 0 then begin    ;Check frame param
  180.     if (frame lt 0) or (frame ge nframes) then $
  181.         message, "Frame number must be from 0 to nframes -1."
  182.     endif
  183.  
  184. if n_elements(order) eq 0 then order = 0  ;Default order
  185.  
  186. j = n_elements(window)
  187. if j gt 0 then begin        ;Copy image from window?
  188.     old_window = !d.window
  189.     wset, window(0)
  190.     if j lt 5 then begin    ;If coords not spec, use all
  191.         p = [ window(0), 0, 0, !d.x_vsize, !d.y_vsize ]
  192.     endif else p = window
  193.     if (p(3) gt xs) or (p(4) gt ys) then $
  194.         message, "Window parameter larger than setup"
  195.     if !d.name eq 'SUN' then begin
  196.         buffer(0, frame * ys) = $
  197.             tvrd(p(1),p(2), p(3), p(4),/order) ;Read from sun
  198.     endif else begin        ;X
  199.         if pwin(frame) lt 0 then begin    ;Create window?
  200.             window, /FREE, xs = xs, ys = ys, /PIXMAP
  201.             pwin(frame) = !d.window
  202.             endif
  203.         wset, pwin(frame)
  204.         device, copy = [ p(1), p(2), p(3), p(4), 0, 0, p(0)]
  205.     endelse        
  206.     if old_window ge 0  then wset, old_window
  207.     return
  208. endif
  209.     
  210. if n_elements(image) ne 0 then begin    ;Load image?
  211.     s = size(image)
  212.     if (s(0) ne 2) or (s(1) gt xs) or (s(2) gt ys) then $
  213.         message, "Image parameter must be 2D of size" $
  214.         + string(xs)+ string(ys)
  215.     if !d.name eq 'SUN' then begin
  216.         if order eq 0 then buffer(0,frame * ys) = reverse(image,2) $
  217.         else buffer(0, frame * ys) = image
  218.     endif else begin
  219.         old_window = !d.window
  220.         if pwin(frame) lt 0 then begin    ;Create window?
  221.             window, /free, xs = xs, ys = ys, /pixmap
  222.             pwin(frame) = !d.window
  223.             endif
  224.         wset, pwin(frame)
  225.         tv,image, order = order
  226.         wset, old_window
  227.     endelse
  228.     return
  229. endif
  230.     
  231.  
  232. if n_elements(rate) ne 0 then delay = 1./rate else delay =0.0
  233. t = systime(1)
  234. j = 0L
  235.  
  236. if !d.name eq 'SUN' then begin
  237.     while 1 do for i=0,nframes-1 do begin
  238.         y = i * ys
  239.         tv, buffer(*, y : y + ys-1), /order
  240.         j = j + 1
  241.         wait, delay
  242.         if get_kbrd(0) ne '' then goto, done
  243.         endfor
  244. endif
  245.  
  246. wset, swin        ;Display animation with X
  247. wshow, swin
  248. while 1 do for i=0, nframes-1 do begin
  249.     device, copy = [ 0, 0, xs, ys, 0, 0, pwin(i)]  ;Copy it
  250.     j = j + 1
  251.     wait, delay
  252.     if get_kbrd(0) ne '' then goto, done
  253.     endfor
  254.  
  255. done: print,j / (systime(1) - t), ' Frames per second.'
  256. end
  257.